package com.jozen.smart_toilet_system.mvp;


import com.jozen.modelbase.netwoek.ApiService;
import com.jozen.modelbase.netwoek.RetrofitManager;
import com.jozen.modelbase.netwoek.RxSchedulers;
import com.jozen.modelbase.netwoek.base.BasePresenter;
import com.jozen.modelbase.netwoek.base.BaseReqModel;
import com.jozen.modelbase.netwoek.base.BaseResult;
import com.jozen.modelbase.netwoek.bean.CityConfModel;
import com.jozen.modelbase.netwoek.bean.HomeModel;
import com.jozen.modelbase.netwoek.bean.SmartToiletInterfaceConfModel;
import com.jozen.modelbase.netwoek.bean.TitleModel;
import com.jozen.modelbase.netwoek.bean.WeatherNowModel;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {

    @Override
    public void getSmartToiletHomePage(BaseReqModel baseReqModel) {
        if (mView != null) {
            HomeContract.View view = mView;
            RetrofitManager.create(ApiService.class)
                    .getSmartToiletHomePage(baseReqModel)
                    .compose(RxSchedulers.applySchedulers())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((BaseResult<HomeModel> baseResult) -> {
                        if (view != null) {
                            view.getSmartToiletHomePageResult(baseResult.getResult());
                        }
                    }, (Throwable throwable) -> {
                        if (view != null) {
                            view.showFailed(throwable.getMessage());
                        }
                    });
        }
    }

    @Override
    public void getSmartToiletNameConf(BaseReqModel baseReqModel) {
        if (mView != null) {
            HomeContract.View view = mView;
            RetrofitManager.create(ApiService.class)
                    .getSmartToiletNameConf(baseReqModel)
                    .compose(RxSchedulers.applySchedulers())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((BaseResult<TitleModel> baseResult) -> {
                        if (view != null) {
                            view.getSmartToiletNameConfResult(baseResult.getResult());
                        }
                    }, (Throwable throwable) -> {
                        if (view != null) {
                            view.showFailed(throwable.getMessage());
                        }
                    });
        }
    }

    @Override
    public void getSmartToiletInterfaceConf(BaseReqModel baseReqModel) {
        if (mView != null) {
            HomeContract.View view = mView;
            RetrofitManager.create(ApiService.class)
                    .getSmartToiletInterfaceConf(baseReqModel)
                    .compose(RxSchedulers.applySchedulers())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((BaseResult<SmartToiletInterfaceConfModel> baseResult) -> {
                        if (view != null) {
                            view.getSmartToiletInterfaceConfResult(baseResult.getResult());
                        }
                    }, (Throwable throwable) -> {
                        if (view != null) {
                            view.showFailed(throwable.getMessage());
                        }
                    });
        }
    }

    @Override
    public void getCityAndKeyConf(BaseReqModel baseReqModel) {
        if (mView != null) {
            HomeContract.View view = mView;
            RetrofitManager.create(ApiService.class)
                    .getCityAndKeyConf(baseReqModel)
                    .compose(RxSchedulers.applySchedulers())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((BaseResult<CityConfModel> baseResult) -> {
                        if (view != null) {
                            view.getCityAndKeyConfResult(baseResult.getResult());
                        }
                    }, (Throwable throwable) -> {
                        if (view != null) {
                            view.showFailed(throwable.getMessage());
                        }
                    });
        }
    }
}